package com.xqledu.admin.handler;

import com.xqledu.admin.entry.pojo.BaseModel;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.stereotype.Component;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author 向其林
 * @version 1.0
 * @description: TODO
 * @date 2023/7/5 0005 21:11
 */
@MappedTypes(Boolean.class)
@MappedJdbcTypes(JdbcType.CHAR)
public class BooleanTypeHandler extends BaseTypeHandler<Boolean> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType) throws SQLException {
        // 将Boolean值转换为数据库需要的类型并设置到PreparedStatement中
        ps.setString(i, parameter ? "1" : "0");
    }

    @Override
    public Boolean getNullableResult(ResultSet rs, String columnName) throws SQLException {
        // 从ResultSet中获取指定列名的值，并将其转换为Boolean类型后返回
        String value = rs.getString(columnName);
        return "1".equals(value);
    }

    @Override
    public Boolean getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        // 从ResultSet中获取指定列索引的值，并将其转换为Boolean类型后返回
        String value = rs.getString(columnIndex);
        return "1".equals(value);
    }

    @Override
    public Boolean getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        // 从CallableStatement中获取指定列索引的值，并将其转换为Boolean类型后返回
        String value = cs.getString(columnIndex);
        return "1".equals(value);
    }
}
